package sxe.core.cs511.team2_2;

/**
 * OVERVIEW: Describes a blob well enough to differentiate two blobs of different objects.
 * The abstraction function has not yet been defined.
 * The representation invariant has not yet been defined.
 * 
 * @author skelly
 *
 */
public class BlobDescription {
	double hist[][][];
	private static final int HIST_LENGTH=12;
	/**
	 * EFFECTS: Creates a BlobDescription from rgb values and label.
	 * 
	 */
	public BlobDescription(int[][][] rgb, int[][] labels, int label, java.awt.Rectangle rect){
		hist=new double[HIST_LENGTH][HIST_LENGTH][HIST_LENGTH];
		double size=0;
		for(int j=rect.y;j<rect.y+rect.height;++j){
			for(int i=rect.x;i<rect.x+rect.width;++i){
				if(labels[j][i]==label){
					double d=rgb[j][i][0]+rgb[j][i][1]+rgb[j][i][2];
					if(d==0) continue;
					double r=rgb[j][i][0]/d*HIST_LENGTH*.99;
					double g=rgb[j][i][1]/d*HIST_LENGTH*.99;
					double b=rgb[j][i][2]/d*HIST_LENGTH*.99;
					hist[(int)r][(int)g][(int)b]+=1;
					++size;
				}
			}
		}
		/*for(int i=0;i<HIST_LENGTH;++i){
			for(int j=0;j<HIST_LENGTH;++j)
				for(int k=0;k<HIST_LENGTH;++k)
					size+=hist[i][j][k]*hist[i][j][k];
		}*/
		//size=java.lang.Math.sqrt(size);
		for(int i=0;i<HIST_LENGTH;++i){
			for(int j=0;j<HIST_LENGTH;++j)
				for(int k=0;k<HIST_LENGTH;++k)
					hist[i][j][k]/=size;
		}
	}
	
	/**
	 * 
	 * EFFECTS: returns value between 0 (worst match) to 1.0 (perfect match)
	 */
	public double distanceTo(BlobDescription d){
		double v=0.;
		for(int i=0;i<HIST_LENGTH;++i){
			for(int j=0;j<HIST_LENGTH;++j)
				for(int k=0;k<HIST_LENGTH;++k)
					v+=java.lang.Math.pow(hist[i][j][k]-d.hist[i][j][k],2);
		}
		return 1-java.lang.Math.sqrt(v)/2;
	}
}
